/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer;

import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.view.MenuItem;
import android.widget.Toast;

import pcgen.android.app.AppUtils;
import pcgen.android.Logger;

import pcgen.CharacterViewer.R;
import pcgen.CharacterViewer.fragments.AppTabContentFragment;
import pcgen.CharacterViewer.fragments.CharacterTabContentFragment;
import pcgen.CharacterViewer.fragments.WebTabContentFragment;
import pcgen.CharacterViewer.resources.GameResource;
import pcgen.CharacterViewer.resources.GameResourceCharacter;

public class ResourceWidgetDefault implements IResourceWidget
{
	public ResourceWidgetDefault(int contentId)
	{
		_contentId = contentId;
	}
	
	public void add(AppTabContentFragment fragment) 
	{
		addEx(fragment);
    }
	
	public void addResourceCharacter(GameResource resource)
	{
		if (!(resource instanceof GameResourceCharacter))
			return;
		
		CharacterTabContentFragment fragment = new CharacterTabContentFragment((GameResourceCharacter)resource);
		fragment.updateUrl(resource.getLocation());
		Tab tab = addEx(fragment);
		_resourceTabs.put(resource,  tab);
	}
	
	public void addResource(GameResource resource)
	{
		WebTabContentFragment fragment = new WebTabContentFragment(resource);
		fragment.updateUrl(resource.getLocation());
		Tab tab = addEx(fragment);
		_resourceTabs.put(resource, tab);
	}
	
	public boolean handleOptionsItemSelected(MenuItem item)
	{
		return false;
	}

	public void initialize(CharacterViewerActivity activity)
	{
		_activity = activity;	
	}
	
	public void removeCurrent() 
    {
        final ActionBar bar = _activity.getActionBar();
        bar.removeTabAt(bar.getTabCount() - 1);
    }

	public void removeResource(GameResource resource) 
    {
		if (!_resourceTabs.containsKey(resource))
			return;
		
		Tab tab = _resourceTabs.get(resource);
        final ActionBar bar = _activity.getActionBar();
        for (int i = 0; i < bar.getTabCount(); i++)
        {
        	Tab temp = bar.getTabAt(i);
        	if (temp == tab)
                bar.removeTabAt(i);
        }
    }

	public void toggle() 
    {
        final ActionBar bar = _activity.getActionBar();

        if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) 
        	toggleOff();
        else
        	toggleOn();
    }

	public void toggleOff() 
    {
        final ActionBar bar = _activity.getActionBar();

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
    }

	public void toggleOn() 
    {
        final ActionBar bar = _activity.getActionBar();

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    }

	public void removeAll() 
	{
		_activity.getActionBar().removeAllTabs();
    }
	
	private Tab addEx(AppTabContentFragment fragment) 
	{
		final ActionBar bar = _activity.getActionBar();
        
        Tab tab = bar.newTab();
        bar.addTab(
        	tab.setText(fragment.getName())
        	.setTabListener(new TabListener(fragment, _contentId)));
        
        return tab;
    }
	
	private CharacterViewerActivity _activity;
	private int _contentId;
	private HashMap<GameResource, Tab> _resourceTabs = new HashMap<GameResource, Tab>();
	
	/**
     * A TabListener receives event callbacks from the action bar as tabs
     * are deselected, selected, and reselected. A FragmentTransaction
     * is provided to each of these callbacks; if any operations are added
     * to it, it will be committed at the end of the full tab switch operation.
     * This lets tab switches be atomic without the app needing to track
     * the interactions between different tabs.
     *
     * NOTE: This is a very simple implementation that does not retain
     * fragment state of the non-visible tabs across activity instances.
     * Look at the FragmentTabs example for how to do a more complete
     * implementation.
     */
    private class TabListener implements ActionBar.TabListener 
    {
        public TabListener(AppTabContentFragment fragment, int contentId) 
        {
        	_contentId = contentId;
            _fragment = fragment;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) 
        {
        	_fragment.onSelected();
            if (!_added) 
            {
                ft.add(_contentId, _fragment, _fragment.getName());
                _added = true;
            }
            else 
                ft.attach(_fragment);
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) 
        {
        	_fragment.onUnselected();
            ft.detach(_fragment);
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) 
        {
        	_fragment.onReselected();
        }
        
        private AppTabContentFragment _fragment;
        private boolean _added;
        private int _contentId;
    }
}